package com.example.btchat.client;

import android.app.Activity;
import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

public class SelectDevice extends Activity
{
	private BluetoothDeviceAdapter adapter;
	private ListView listView;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_select_device);
		
		adapter = new BluetoothDeviceAdapter(this);
		
		Button button = new Button(this);
		button.setText(R.string.title_scan_for_devices);
		button.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				scanForDevices();
			}
		});
		
		listView = (ListView) findViewById(R.id.list_view);
		listView.addFooterView(button);
		listView.setFooterDividersEnabled(true);
		listView.setAdapter(adapter);
		
		listView.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
			{
				SelectDevice.this.onItemClick(position);
			}
		});
	}
	
	protected void onItemClick(int position)
	{
		BluetoothDevice device = (BluetoothDevice) adapter.getItem(position);
		Intent in = new Intent();
		in.putExtra("device", device.getAddress());
		setResult(RESULT_OK, in);
		finish();
	}
	
	protected void scanForDevices()
	{
		adapter.searchForDevices();
	}
}
